$(function(){
	//初始化
	init();
	bindFun();
});
var contractId;
var policyRef;
var result;
function init(){
	contractId = sessionStorage.contractId;
	policyRef  = sessionStorage.policyRef;
	//初始化数据
	var da ={};
	da.contractId=contractId;
	padMarketing.ajaxNormal(
			"/pad_marketing/qualityCheck/initMyContent.action",
			da,
			function(data){
				result = data.result;
				handle(result);
				if(myScroll) {
					myScroll.refresh();
				}
				setTimeout(function(){
					initSign(policyRef,'1');
				}, 1000);
				
			}
	);
};
function bindFun(){
	
};
//处理数据  显示界面
function handle(result){
	//操作选项
	if(result.other!="Y"){
		$("#otherSign").hide();
	}
	if(result.tb!="Y"){
		$("#sign").hide();
	}
	//实际拍照
	if(result.sign=="Y"){
		$("#signOne").html('<img src="../html/img/signIdY.png" width="98" >');
		$("#signTwo").html('<img src="../html/img/signIdY.png" width="98" >');
	}
	if(result.otherSign=="Y"){
		$("#signThree").html('<img src="../html/img/signIdY.png" width="98" >');
		$("#signFour").html('<img src="../html/img/signIdY.png" width="98" >');
	}
	
}
//下一步界面
function next(){
	// flag表示签字 true签过
	var flag = checkLocation();
	if(flag){
		if(result.add=="Y"){
			location.href="furtherInformation.action?contractId="+contractId+"&policyRef="+policyRef+"&url=bodySign.action";
			return;
		}else{
			location.href="affirmNoteReply.action?contractId="+contractId+"&policyRef="+policyRef+"&url=bodySign.action";
			return;
		}
	}
	//合成pdf
	//1 校验
	if(result.tb=="Y"&&result.sign!="Y"){
		var sign = $("#sign").attr("style");
		if('display: none;'==sign){
			return;
		}
		//投保单--投保人签名
		var sign1 = $("#sign1").attr("style");
		if('display: none;'!=sign1 ){
			alertMsg('请完成投保单投保人签名');
			return;
		}
		//校验投保单--投保人拍照
		if(''==src1){
			alertMsg('请完成投保单投保人清晰的胸部以上正面照拍摄！');
			return;
		}
		
		//投保单--经纪人/营销人员签名
		var sign3 = $("#sign3").attr("style");
		if('display: none;'!=sign3){
			alertMsg('请完成投保单营销人员签名！');
			return;
		}
		//校验投保单--经纪人/营销人员拍照
		if(''==src3){
			alertMsg('请完成投保单营销人员清晰的胸部以上正面照拍摄！');
			return;
		}
	}
	if(result.other=="Y"&&result.otherSign!="Y"){
		//所需签名资料签名
		var otherSign = $("#otherSign").attr("style");
		var sign4 = $("#sign4").attr("style");
		var sign5 = $("#sign5").attr("style");
		
		//所需签名资料投保人签名
		if('display: none;'!=otherSign && ('display: none;'!=sign4)){
			alertMsg('请完成所需签名资料（投保人）签名！');
			return;
		}

		//所需签名资料投保人拍照
		if('display: none;'!=otherSign && (''==src4)){
			alertMsg('请完成所需签名资料（经纪人/营销人员）清晰的胸部以上正面照拍摄！ ');
			return;
		}
		
		//所需签名资料经纪人/营销人员签名
		if('display: none;'!=otherSign && ('display: none;'!=sign5)){
			alertMsg('请完成所需签名资料（经纪人/营销人员）签名！');
			return;
		}
		
		//所需签名资料经纪人/营销人员拍照
		if('display: none;'!=otherSign && (''==src5)){
			alertMsg('请完成所需签名资料（经纪人/营销人员）清晰的胸部以上正面照拍摄！ ');
			return;
		}
	}
	
	//调壳生成加密字符串
	finishSign("doSign");
}

/**
 * 壳回调函数，展示签字图片
 * 
 * @param src             goSign1('showSign','pdf2','0','img4')
 * @param pdfNum
 * @param siger
 * @param imgNum
 */
function showSign(src,pdfNum,siger,imgNum){
	
	var num = imgNum.substr(3,1);
	$("#sign"+num).hide();
	var img=document.getElementById(imgNum);
	src = 'data:image/png;base64,' + src;
	img.src = src;
	if(myScroll)
		myScroll.refresh();
}

/**
 * 展示拍照头像
 *                takeSignPhoto('showPhoto','pdf1','0','pho1')
 * @param src
 * @param imgNum
 */
var src1 = "";  
var src3 = "";
var src4 = "";
var src5 = "";
function showPhoto(src,imgNum){
	if('pho1'==imgNum)src1 = src;
	if('pho3'==imgNum)src3 = src;
	if('pho4'==imgNum)src4 = src;
	if('pho5'==imgNum)src5 = src;
	var img=document.getElementById(imgNum);
	src = 'data:image/png;base64,' + src;			
	img.src = src;
	if(myScroll)
		myScroll.refresh();
}
function doSign(pdf1,pdf2){
	var signKey=contractId+"NEWTOUCH";
	$.get('/pad_marketing/main/getSign.action',{'jsonParam':JSON.stringify({signKey:signKey})},function(data){
		
		var pdfURL1 = data.address+"/ProposalByContractId?contractId="+contractId+"&partner=NEWTOUCH&sign="+data.sign;
		var pdfURL2 = data.address+"/OtherSignDocumentsByContractId?contractId="+contractId+"&partner=NEWTOUCH&sign="+data.sign;
		var photoList1 = [src1,src3];
		var photoList2 = [src4,src5];
		var pdfMessageList = [];
		if(result.other=="Y"&&result.otherSign!="Y"){
			pdfMessageList.push({"pdfType":"1169","pdfURL":pdfURL2,"encode":pdf2,"photoList":photoList2});
		}
		if(result.tb=="Y"&&result.sign!="Y"){
			pdfMessageList.push({"pdfType":"1151","pdfURL":pdfURL1,"encode":pdf1,"photoList":photoList1});
		}
		var params={"policyRef":policyRef,"pdfMessageList":pdfMessageList,"insured":contractId};
		
		padMarketing.ajaxNormal('/pad_marketing/qualityCheck/QCBodySignCA.action',params,
				function(data){
						//储存操作
						if(result.other=="Y"){
							result.otherSign="Y";
						}
						if(result.tb=="Y"){
							result.sign="Y";
						}
						padMarketing.ajaxNormal(
								"/pad_marketing/qualityCheck/updateMyContent.action",
								result,
								function(data){
									if(result.add=="Y"){
										location.href="furtherInformation.action?contractId="+contractId+"&policyRef="+policyRef;
									}else{
										location.href="affirmNoteReply.action?contractId="+contractId+"&policyRef="+policyRef;
									}
								}
						);
					},
				function(data){
					
				},
				true,//是否异步
				true,//是否显示加载电波图
				'不用美图都那么帅，不过字该练练啦！'//电波图中的话术
		);
	});
}

//查看pdf
function showNotePdf(flag){
	var signKey=contractId+"NEWTOUCH";
	$.get('/pad_marketing/main/getSign.action',{'jsonParam':JSON.stringify({signKey:signKey})},function(data){
		var pdfurl=data.address+flag+"?contractId="+contractId+"&partner=NEWTOUCH&sign="+data.sign;
		var param=0;
		var ifSign ="N";
		//调壳弹出新页面，参数说明：1：回调函数名称，2：要展示的地址，3：回调函数参数，4：是否需要签字
		gotoPDF('pdfRefresh',pdfurl,param,ifSign);
	});
}
function checkLocation(){
	if(result.tb=="Y"&&result.sign!="Y"){
		return false;
	}
	if(result.other=="Y"&&result.otherSign!="Y"){
		return false;
	}
	return true;
}